#import "EzvizFlutterPlugin.h"
#import "FlutterPlatformViewFactory.h"
#import "TestFlutterPluginView.h"
#import <EZOpenSDKFramework/EZOpenSDKFramework.h>

@implementation EzvizFlutterPlugin
{
    NSString *accessToken;
    NSString *deviceSerial;
    NSInteger cameraNo;//通道号
}




+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"ezviz_flutter_plugin"
            binaryMessenger:[registrar messenger]];
  EzvizFlutterPlugin* instance = [[EzvizFlutterPlugin alloc] init];
  [registrar addMethodCallDelegate:instance channel:channel];
    
    
 FlutterPlatformViewFactory * flutterPlatformViewFactory = [[FlutterPlatformViewFactory alloc] initWithMessager:registrar.messenger];
 [registrar registerViewFactory:flutterPlatformViewFactory withId:@"ezviz_flutter_plugin_ios_view"];
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    
    if ([call.method isEqual: @"getPlatformVersion"]) { //获取版本号
        
        result([@"iOS:" stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
        
    } else if ([call.method isEqualToString: @"startToPlay"]) { // 开始播放
      
        [EZOpenSDK setAccessToken:call.arguments[@"accessToken"]];
        
        [EZOpenSDK enableP2P:YES];
        
        deviceSerial = call.arguments[@"deviceSerial"];
        
        cameraNo = [call.arguments[@"cameraNo"] integerValue];
        
        accessToken = call.arguments[@"accessToken"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"infoNotification" object:nil userInfo:call.arguments];
  
        
    } else if ([call.method isEqual: @"logout"]) { // 退出
        
        [EZOpenSDK logout:^(NSError *error) {
            NSLog(@"%@",error);
        }];
        
    } else if ([call.method isEqual: @"openSound"]) { //开始声音
        
        NSDictionary *dict = [NSDictionary dictionaryWithObject:@"openSound" forKey:@"Sound"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"infoNotificationSound" object:nil userInfo:dict];

    }else if ([call.method isEqual: @"closeSound"]) { // 关闭声音
        NSDictionary *dict = [NSDictionary dictionaryWithObject:@"closeSound" forKey:@"Sound"];

        [[NSNotificationCenter defaultCenter] postNotificationName:@"infoNotificationSound" object:nil userInfo:dict];

    }else if ([call.method isEqual: @"setDirection"]) { //方向移动
        NSString * directionStr = call.arguments[@"direction"];
        NSNumber *number  = call.arguments[@"stop"];
        
        int ivalue = [directionStr intValue];
                
        [self ptzStart:ivalue stop:(number.integerValue == 1)?YES:NO];
        
    }else{ //default
            
    }
}

#pragma mark - 按钮的点击事件action
- (void)ptzStart:(int)sender stop:(bool)stop
{
    EZPTZCommand command;
    switch (sender) {
        case 0:
        
            command = EZPTZCommandUp;
            break;
        case 1:
            command = EZPTZCommandDown;
            break;
        case 2:
            command = EZPTZCommandLeft;
            break;
        case 3:
            command = EZPTZCommandRight;
            break;
        default :
            command = EZPTZCommandUp;
            break;;
    }
    
    
    [EZOpenSDK controlPTZ:deviceSerial cameraNo:cameraNo command:command action:stop?2:1 speed:1 result:^(NSError *error) {
        NSLog(@"错误 %@",error);
    }];
}


// 初始化
-(void) initWithAppkey: (NSString *)appKey result: (FlutterResult) result {
   /** * sdk日志开关，正式发布需要去掉 */
    [EZOpenSDK setDebugLogEnable:YES];
    [EZOpenSDK initLibWithAppKey:@"877fa59eea614e0e8ee2bd46da610d04"];
    [EZHCNetDeviceSDK initSDK];
    [EZOpenSDK enableP2P:YES];
 }





@end
